﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using LowLevelGraphics;
using LowLevelGraphics.Filter;
using LowLevelGraphics.Extractors;

namespace ImageRecognition2.Forms
{
    /// <summary>
    /// Crop Image
    /// </summary>
    public partial class FormCrop : FormFilter
    {
        protected Rectangle m_RectangleCrop = Rectangle.Empty;

        /// <summary>
        /// 
        /// </summary>
        public FormCrop()
        {
            InitializeComponent();
            this.Shown += new EventHandler(FormCrop_Shown);
            nudXStart.ValueChanged += new EventHandler(nud_ValueChanged);
            nudYEnd.ValueChanged += new EventHandler(nud_ValueChanged);
            nudYStart.ValueChanged += new EventHandler(nud_ValueChanged);
            nudXEnd.ValueChanged += new EventHandler(nud_ValueChanged);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void nud_ValueChanged(object sender, EventArgs e)
        {
            Crop();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void FormCrop_Shown(object sender, EventArgs e)
        {
            m_ImageOriginal =  (Image)MainFormSingleton.ActivePictureBox.BackgroundImage.Clone();
            int nWidth = m_ImageOriginal.Width;
            int nHeight = m_ImageOriginal.Height;

            nudXStart.Maximum = nWidth;
            nudYEnd.Maximum = nHeight;
            nudXEnd.Maximum = nWidth;
            nudYEnd.Maximum = nHeight;
        }

        /// <summary>
        /// Crop Function
        /// </summary>
        protected void Crop()
        {
            if (chkbAutomatic.Checked)
            {
                m_RectangleCrop = AutoCrop();
            }
            else
            {
                m_RectangleCrop = ManualCrop(m_RectangleCrop);
            }
            if (chkbPreview.Checked)
            {
                Bitmap bitmap = (Bitmap)m_ImageOriginal.Clone();
                Graphics g = Graphics.FromImage(bitmap);
                g.DrawRectangle(Pens.Red, m_RectangleCrop);
                this.pictureBox1.BackgroundImage = bitmap;
            }
        }

        protected Rectangle ManualCrop(Rectangle rect)
        {
            if (chkbPreview.Checked)
            {
                int XStart = (int)nudXStart.Value;
                int YStart = (int)nudYStart.Value;
                int XEnd = (int)nudXEnd.Value;
                int YEnd = (int)nudYEnd.Value;
                rect = Rectangle.FromLTRB(XStart, YStart, XEnd, YEnd);
            }
            return rect;
        }

        protected Rectangle AutoCrop()
        {
            List<IFilter> aFilterList = new List<IFilter>();
            foreach (IFilter filter in listBox1.Items)
            {
                aFilterList.Add(filter);
            }
            AutoCrop crop = new AutoCrop(aFilterList);
            return crop.CropRectangle;
        }

        private void cbOK_Click(object sender, EventArgs e)
        {
            MainFormSingleton.ActivePictureBox.BackgroundImage = BitmapFunctions.GetPicturePart(MainFormSingleton.ActivePictureBox.BackgroundImage, m_RectangleCrop);
            MainFormSingleton.ActivePictureBox.Invalidate();
        }
    }
}
